﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TEngine;
using GameLogic;
using System.Threading;
using GameConfig.item;


public class Player : MonoBehaviour
{
    //移速
    public float Speed { get; set; }
    public float SpeedMultiple { get; set; }

    //状态
    [HideInInspector]
    public bool isLive;
    bool isControllable;
    bool isInvincible;
    Vector2 moveInput;


    [Header("自身")]
    public Transform head;
    public Transform body;
    Rigidbody2D myRigidbody;
    SpriteRenderer bodyRanderer;
    SpriteRenderer headRanderer;
    Animator wholeAnimation;
    Animator headAnimation;
    Animator bodyAnimation;

    void Awake()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        headRanderer = head.GetComponent<SpriteRenderer>();
        bodyRanderer = body.GetComponent<SpriteRenderer>();
        wholeAnimation = GetComponent<Animator>();
        headAnimation = head.GetComponent<Animator>();
        bodyAnimation = body.GetComponent<Animator>();
    }

    private void OnEnable()
    {
        GameEvent.AddEventListener<Vector3, ItemTb>(ILoginUI_Event.CallMouseClickedEvent, OnMouseClickedEvent);
    }

    private void OnDisable()
    {
        GameEvent.RemoveEventListener<Vector3, ItemTb>(ILoginUI_Event.CallMouseClickedEvent, OnMouseClickedEvent);
    }

    private void OnMouseClickedEvent(Vector3 vector, ItemTb details)
    {
        //if (details.ItemType != ItemType.Seed && details.itemType != ItemType.Commodity && details.itemType != ItemType.Furniture)
        //{
        //    mouseX = vector.x - transform.position.x;
        //    mouseY = vector.y - (transform.position.y + 0.85f);
        //    if (Mathf.Abs(mouseX) > Mathf.Abs(mouseY))
        //    {
        //        mouseY = 0;
        //    }
        //    else
        //    {
        //        mouseX = 0;
        //    }
        //    StartCoroutine(UseToolRoutine(vector, details));
        //}
        //else
        //{
        //    GameEvent.Get<ILoginUI>.CallExecuteActionAfterAnimation(vector, details);
        //}
        GameEvent.Get<ILoginUI>().ExecuteActionAfterAnimation(vector, details);
    }

    void Start()
    {

        PlayerInitialize();
    }

    void Update()
    {

        if (!isControllable) { return; }
        UpdateMovement();
        UpdateAnimator();
    }

    /// <summary>
    /// 移动
    /// </summary>
    void UpdateMovement()
    {
        var h = Input.GetAxis("Horizontal");
        var v = Input.GetAxis("Vertical");
        moveInput = h * Vector2.right + v * Vector2.up;
        //归一化，取值0 - 1,斜着走的速度不会超过移动速度。
        if (moveInput.magnitude > 1f)
        {
            moveInput.Normalize();
        }
        //myRigidbody.velocity = moveInput * Speed * SpeedMultiple * 1.7f;
        myRigidbody.velocity = moveInput * (0.5f + 0.5f * Speed) * SpeedMultiple * 1.7f;
    }

    /// <summary>
    /// 更新动画
    /// </summary>
    void UpdateAnimator()
    {
        if (moveInput.x < 0) { bodyRanderer.flipX = true; }
        if (moveInput.x > 0) { bodyRanderer.flipX = false; }
        bodyAnimation.SetFloat("Up&Down", Mathf.Abs(moveInput.y));
        bodyAnimation.SetFloat("Left&Right", Mathf.Abs(moveInput.x));
    }

    /// <summary>
    /// 状态初始化
    /// </summary>
    public void PlayerInitialize()
    {

        Speed =2f;
        SpeedMultiple = 2f;

        isLive = true;
        isControllable = true;
        isInvincible = false;
        bodyRanderer.enabled = true;
        headRanderer.enabled = true;
        wholeAnimation.SetBool("isLive", true);
        myRigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
    }
}